package com.itheima.sfbx.insurance.handler.impl;

import cn.hutool.core.util.ObjectUtil;
import com.itheima.sfbx.framework.commons.constant.insure.InsureConstant;
import com.itheima.sfbx.insurance.dto.CustomerRelationVO;
import com.itheima.sfbx.insurance.dto.DoInsureVo;
import com.itheima.sfbx.insurance.handler.InsureHandler;
import com.itheima.sfbx.insurance.pojo.CustomerRelation;
import com.itheima.sfbx.insurance.service.ICustomerRelationService;
import com.itheima.sfbx.insurance.service.impl.CustomerRelationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @ClassName AccidentInsureHandler.java
 * @Description 保障型意外类：保险投保、创建订单、保费计算
 */
@Service
public class AccidentInsureHandler extends SafeguardInsureHandler implements InsureHandler {
    @Autowired
    private ICustomerRelationService customerRelationService;
    @Override
    public String doPremium(DoInsureVo doInsureVo) {
        CustomerRelationVO relationVO = customerRelationService.findById(doInsureVo.getCustomerRelationIds().get(0));

        //判断投保人是否为null，或者 没有写收入，或者收入高于100万
        if (ObjectUtil.isNotEmpty(relationVO)) {
            //高于100万或者没有填写的人直接抛出异常
            if (relationVO.getIncome().compareTo(new BigDecimal("1000000.0")) > 0 || relationVO.getIncome() == null) {
                throw new RuntimeException("杀手不能投保意外险！");
            }
        }
        return super.doPremium(doInsureVo);
    }
}
